import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/index',
    redirect:'/index/home',
    component: () => import('../pages/index/index'),
    children: [
      {
        path: 'home',
        component: () => import('../pages/home/home'),
        meta: {title:'首页',back:false,register:false},
      },
      {
        path: 'cate',
        component: () => import('../pages/cate/cate'),
        name: '分类',
        meta: {title:'分类',back:false,register:false},
      },
      {
        path: 'mine',
        component: () => import('../pages/mine/mine'),
        name: '我的',
        meta: {title:'我的',back:false,register:false},
      },
      {
        path: 'shop',
        component: () => import('../pages/shop/shop'),
        name: '购物车',
        meta: {title:'购物车',back:false,register:false},
      },
    ],
  },
  {
    path: '/login',
    component: () => import('../pages/login/login'),
    name: '登录',
    meta: {title:'登录',back:false,register:false},
    children: [],
  },
  {
    path: '/register',
    component: () => import('../pages/register/register'),
    name: '注册',
    meta: {title:'注册',back:true},
    children: [],
  },
  {
    path: '/list',
    component: () => import('../pages/list/list'),
    name: '商品列表',
    meta: {title:'商品列表',back:true},
    children: [],
  },
  {
    path: '/search',
    component: () => import('../pages/search/search'),
    name: '搜索',
    meta: {title:'搜索',back:true},
    children: [],
  },
  {
    path: '/detail',
    component: () => import('../pages/detail/detail'),
    name: '商品详情',
    meta: {title:'商品详情',back:true},
    children: [],
  },
  {
    path: '/order',
    component: () => import('../pages/order/order'),
    name: '订单详情',
    meta: {title:'提交订单',back:true},
    children: [],
  },
  {
    path: '/aList',
    component: () => import('../pages/addressList/addresList'),
    name: '地址列表',
    meta: {title:'地址列表',back:true},
  },
  {
    path: '/addressAdd',
    component: () => import('../pages/addressAdd/addressAdd'),
    name: '地址详情',
    meta: {title:'地址详情',back:true},
  },
  {
    path: '/pay',
    component: () => import('../pages/pay/pay'),
    name: '支付订单',
    meta: {title:'支付订单'},
  },
  
  {
    path:'*',
    redirect:'/index'
  }
]


const router = new VueRouter({
  routes,
  scrollBehavior(to, from, savePosition) {
    return savePosition ? savePosition : { x: 0, y: 0 }
  }
})

router.beforeEach((to,from,next)=>{
  if(to.path == '/login' || to.path == '/register'){
    next()
    return;
  }
  let isLogin = localStorage.getItem('isLogin');
  if(isLogin){
    next();
  }else{
    next("/login")
  }
})
export default router
